#include<iostream>
#include<mysql/mysql.h>
#include<string>

const std::string host="127.0.0.1";
const std::string user="root";
const std::string passwd="13623964732";
const std::string dataname="test_db";

int main()
{

    MYSQL* mysql=mysql_init(NULL);
    if(nullptr==mysql)
    {
        std::cout<<"init mysql filed"<<std::endl;
        return -1;
    }

    //数据库连接
    MYSQL* mysql_connect=mysql_real_connect(mysql,host.c_str(),user.c_str(),passwd.c_str(),dataname.c_str(),3306,NULL,0 );

    if(mysql_connect==nullptr)
    {
        std::cout<<" content mysql error"<<std::endl;
        return -1;
    }

    //设置字符集
    if(mysql_set_character_set(mysql,"utf8")!=0)
    {
        std::cout<<"character set error"<<std::endl;
        return -1;
    }


    //向数据库添加数据
    std::string sql="insert into stu values(null,18,'小明'), (2,19,'小马')";
    int ret=mysql_query(mysql,sql.c_str());

    if(ret!=0)
    {
        std::cout<<"mysql query error"<<std::endl;
        return -1;
    }


    mysql_close(mysql);
    return 0;
}